package com.hyjx.framework.common.tags;

import com.hyjx.framework.common.util.SpringBeanUtil;
import com.hyjx.framework.component.codetable.CodeBean;
import com.hyjx.framework.component.codetable.CodeTableManager;
import com.hyjx.framework.component.field.ExtField;
import com.hyjx.framework.component.field.ExtFieldService;
import com.hyjx.framework.orm.dao.IBaseDao;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 自定义扩展列编辑标签
 */
public class ExtFieldTag extends TagSupport {
	private String tableName;
	private String operateType;
	private String dataId;
	private String pkName;

	/**
	 * 执行标签
	 * 
	 * @return
	 */
	@Override
	public int doStartTag() {
		ExtFieldService extFieldService = (ExtFieldService) SpringBeanUtil.getBean("extFieldServiceImpl");
		IBaseDao baseDao = (IBaseDao) SpringBeanUtil.getBean("baseDao");

		List<ExtField> fields = extFieldService.getFieldsByTableName(tableName);// 查出所有扩展字段
		List<ExtField> newfield = new ArrayList<ExtField>();
		;
		if (fields == null || fields.size() == 0) {
			return SKIP_BODY;
		}
		Map<String, Object> extData = null;
		if (!"1".equals(operateType)) {
			String sql = "select 1 as xxx";
			for (int i = 0; i < fields.size(); i++) {
				sql += "," + fields.get(i).getEnName();
			}
			sql += " from " + tableName + " where " + pkName + "='" + dataId + "'";
			List<Map<String, Object>> list = baseDao.queryListBySql(sql);
			if (list != null && list.size() > 0) {
				extData = list.get(0);
			}
		}

		StringBuffer html = new StringBuffer();
		for (int i = 0; i <= fields.size() - 1; i++) {
			ExtField field = fields.get(i);
			String value = "";
			if ("1".equals(operateType)) {
				value = "";
			} else {
				if (extData.containsKey(field.getEnName())) {
					if (extData.get(field.getEnName()) == null) {
						value = "";
					} else {
						value = extData.get(field.getEnName()).toString();
					}
				}
			}

			if ("13".equals(field.getEditType())) {
				html.append("<input name=\"extData['" + field.getEnName() + "']\" type=\"hidden\" value=\"" + value+ "\" />");

			} else {
				newfield.add(field);
			}

		}

		for (int i = 0; i <= newfield.size() - 1; i++) {
			ExtField field = newfield.get(i);
			String value = "";
			if (i % 2 == 0) {
				html.append("<tr>");
			}
			if ("1".equals(operateType)) {
				value = "";
			} else {
				if (extData.containsKey(field.getEnName())) {
					if (extData.get(field.getEnName()) == null) {
						value = "";
					} else {
						value = extData.get(field.getEnName()).toString();
					}
				}
			}
			if ("12".equals(field.getEditType())) {
				// 下拉码表
				List<CodeBean> codeList = CodeTableManager.getTableList(field.getCodeTable());
				html.append("<th >" + field.getCnName() + "</th>");

				if ("3".equals(operateType)) {
					html.append("<td > " + CodeTableManager.getNameByCode(field.getCodeTable(), value));
					html.append("</td>");
				} else {
					html.append(
							"<td  ><select id=\"" + field.getCnName() + "\"  class=\"easyui-combobox\" name=\"extData['"
									+ field.getEnName() + "']\" style=\"width:200px;\">");
					if (codeList != null) {
						for (int k = 0; k < codeList.size(); k++) {
							String codeValue = codeList.get(k).getCode();
							String codeName = codeList.get(k).getName();
							html.append("<option value=\"" + codeValue + "\" >" + codeName + "</option>");
						}
					}
					html.append("</select></td>");
					html.append("<script type=\"text/javascript\">$(function(){ $(\"#" + field.getCnName()
							+ "\").combobox('setValue','" + value + "');}); </script>");
				}
			} else {
				// 普通文本框
				html.append("<th >" + field.getCnName() + "</th>");
				if ("3".equals(operateType)) {
					html.append("<td > " + value);
					html.append("</td>");
				} else {
					html.append("<td ><input class=\"easyui-textbox\" name=\"extData['" + field.getEnName()
							+ "']\" type=\"text\" value=\"" + value + "\" /> </td>");
				}
			}
        
			if ((i == newfield.size() - 1) && (i % 2 == 0)) {
				html.append("<th ></th>").append("<td ></td> </tr>");
			} else if (i % 2 == 1) {
				html.append("</tr>");
			}
		}
		html.append("<input name=\"extData['_tableName']\" type=\"hidden\" value=\"" + tableName + "\">");
		html.append("<input name=\"extData['_pkName']\" type=\"hidden\" value=\"" + pkName + "\">");
		html.append("<input name=\"extData['_dataId']\" type=\"hidden\" value=\"" + dataId + "\">");

		try {
			JspWriter out = pageContext.getOut();
			out.println();
			out.print(html.toString());
			out.println();
		} catch (Exception e) {
		}
		return SKIP_BODY;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getOperateType() {
		return operateType;
	}

	public void setOperateType(String operateType) {
		this.operateType = operateType;
	}

	public String getDataId() {
		return dataId;
	}

	public void setDataId(String dataId) {
		this.dataId = dataId;
	}

	public String getPkName() {
		return pkName;
	}

	public void setPkName(String pkName) {
		this.pkName = pkName;
	}
}